package com.cinc.mdm.modules.workCenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cinc.mdm.modules.workCenter.entity.MdmWorkCenter;
import com.cinc.mdm.modules.workCenter.mapper.MdmWorkCenterMapper;
import com.cinc.mdm.modules.workCenter.model.MdmWorkCenterTreeModel;
import com.cinc.mdm.modules.workCenter.service.IMdmWorkCenterService;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 工作中心
 * @Author: jeecg-boot
 * @Date: 2021-12-26
 * @Version: V1.0
 */
@Service
public class MdmWorkCenterServiceImpl extends ServiceImpl<MdmWorkCenterMapper, MdmWorkCenter> implements IMdmWorkCenterService {

    @Override
    public List<MdmWorkCenterTreeModel> queryTreeList(String name) {
        List<MdmWorkCenterTreeModel> treeList = new ArrayList<>();
        if (oConvertUtils.isEmpty(name)) {
            // 树形列表
            treeList = baseMapper.selectTreeTopList();
            for (MdmWorkCenterTreeModel topData : treeList) {
                setWorkCenterTree(topData);
            }
        } else {
            // 普通列表
            MdmWorkCenter workCenterRequest = new MdmWorkCenter();
            workCenterRequest.setName(name);
            treeList = baseMapper.selectTreeList(workCenterRequest);
        }

        return treeList;
    }

    @Transactional
    @Override
    public void delete(String id) {
        List<String> idList = new ArrayList<>();
        idList.add(id);
        this.checkChildrenExists(id, idList);
        this.removeByIds(idList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteBatch(List<String> ids) {
        List<String> idList = new ArrayList<String>();
        for(String id: ids) {
            idList.add(id);
            this.checkChildrenExists(id, idList);
        }
        this.removeByIds(idList);
    }

    /**
     * 获取所有子记录ID
     * @param id
     * @param idList
     */
    private void checkChildrenExists(String id, List<String> idList) {
        LambdaQueryWrapper<MdmWorkCenter> query = new LambdaQueryWrapper<>();
        query.eq(MdmWorkCenter::getPid,id);
        List<MdmWorkCenter> workCenterList = this.list(query);
        if(workCenterList != null && workCenterList.size() > 0) {
            for(MdmWorkCenter workCenter : workCenterList) {
                idList.add(workCenter.getId());
                this.checkChildrenExists(workCenter.getId(), idList);
            }
        }
    }

    private void setWorkCenterTree(MdmWorkCenterTreeModel workCenterTreeModel) {
        MdmWorkCenter workCenterRequest = new MdmWorkCenter();
        workCenterRequest.setPid(workCenterTreeModel.getId());
        List<MdmWorkCenterTreeModel> childList = baseMapper.selectTreeList(workCenterRequest);
        if (oConvertUtils.listIsNotEmpty(childList)) {
            workCenterTreeModel.setChildList(childList);
            for (MdmWorkCenterTreeModel child : childList) {
                setWorkCenterTree(child);
            }
        }
    }

}
